package com.imedway.ihe.dtz.xds.dao.camel;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.imedway.ihe.dtz.xds.entity.camel.XdsAssociationPO;
import com.imedway.ihe.dtz.xds.entity.camel.XdsCache;
import com.imedway.ihe.dtz.xds.entity.camel.XdsDocumentEntryPO;
import com.imedway.ihe.dtz.xds.entity.camel.XdsFolderPO;
import com.imedway.ihe.dtz.xds.entity.camel.XdsRepositoryPO;
import com.imedway.ihe.dtz.xds.entity.camel.XdsSubmissionSetPO;

import lombok.extern.slf4j.Slf4j;

/**
 * @author dtz <br/>
 * @date 2024/7/4
 */
@Slf4j
@Repository
public class SaveBaseDao_CAMEL {
	
	@Resource
	private JdbcTemplate jdbcTemplate;
		
	@Resource
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
	
    public void saveSubmissionSet(XdsSubmissionSetPO po)  {		
		
		StringBuilder sql = new StringBuilder();
		sql.append(" INSERT INTO camel_xdssubmissionset (");
		sql.append("createTime,updateTime,contentTypeCode,sourceId,submissionTime,availabilityStatus,entryUuid,patientId,uniqueId,homeCommunityId,logicalUuid,version");
		sql.append(") VALUES (");
		sql.append(":createTime,:updateTime,:contentTypeCode,:sourceId,:submissionTime,:availabilityStatus,:entryUuid,:patientId,:uniqueId,:homeCommunityId,:logicalUuid,:version");
		sql.append(")");

	    Map<String, Object> parameters = new HashMap<>();
	    parameters.put("createTime", po.getCreateTime());
	    parameters.put("updateTime", po.getUpdateTime());
	    parameters.put("contentTypeCode",po.getContentTypeCode());
	    parameters.put("sourceId", po.getSourceId());
	    parameters.put("submissionTime", po.getSubmissionTime());
	    parameters.put("availabilityStatus", po.getAvailabilityStatus());
	    parameters.put("entryUuid", po.getEntryUuid());
	    parameters.put("patientId", po.getPatientId());
	    parameters.put("uniqueId",po.getUniqueId());
	    parameters.put("homeCommunityId", po.getHomeCommunityId());
	    parameters.put("logicalUuid", po.getLogicalUuid());
	    parameters.put("version", po.getVersion());

		log.info(sql.toString());
	    namedParameterJdbcTemplate.update(sql.toString(), parameters);

	}
	
	public void saveXdsFolderPO(XdsFolderPO po)  {

		StringBuilder sql = new StringBuilder();
		sql.append(" INSERT INTO camel_xdsfolder (");
		sql.append("createTime,updateTime,availabilityStatus,entryUuid,patientId,uniqueId,homeCommunityId,"
				+ "logicalUuid,version,code,schemeName,lastUpdateTime,titleValue,commentValue");
		sql.append(") VALUES (");
		sql.append(":createTime,:updateTime,:availabilityStatus,:entryUuid,:patientId,:uniqueId,:homeCommunityId,"
				+ ":logicalUuid,:version,:code,:schemeName,:lastUpdateTime,:titleValue,:commentValue");
		sql.append(")");

	    Map<String, Object> parameters = new HashMap<>();
	    parameters.put("createTime", po.getCreateTime());
	    parameters.put("updateTime", po.getUpdateTime());
	    parameters.put("availabilityStatus", po.getAvailabilityStatus());
	    parameters.put("entryUuid", po.getEntryUuid());
	    parameters.put("patientId", po.getPatientId());
	    parameters.put("uniqueId", po.getUniqueId());
	    parameters.put("homeCommunityId",po.getHomeCommunityId());
	    parameters.put("logicalUuid", po.getLogicalUuid());
	    parameters.put("version", po.getVersion());
	    parameters.put("code", po.getCode());
	    parameters.put("schemeName", po.getSchemeName());
	    parameters.put("lastUpdateTime", po.getLastUpdateTime());
	    parameters.put("titleValue", po.getTitleValue());
	    parameters.put("commentValue", po.getCommentValue());
		log.info(sql.toString());
	    namedParameterJdbcTemplate.update(sql.toString(), parameters);
	}
	
	public void saveXdsAssociationPO(XdsAssociationPO po)  {

		StringBuilder sql = new StringBuilder();
		sql.append(" INSERT INTO camel_xdsassociation (");
		sql.append("createTime,updateTime,targetUuid,sourceUuid,associationType,associationLabel,entryUuid,previousVersion,originalStatus,newStatus,associationPropagation,availabilityStatus");
		sql.append(") VALUES (");
		sql.append(":createTime,:updateTime,:targetUuid,:sourceUuid,:associationType,:associationLabel,:entryUuid,:previousVersion,:originalStatus,:newStatus,:associationPropagation,:availabilityStatus");
		sql.append(")");

	    Map<String, Object> parameters = new HashMap<>();
	    parameters.put("createTime", po.getCreateTime());
	    parameters.put("updateTime", po.getUpdateTime());
	    parameters.put("targetUuid",po.getTargetUuid());
	    parameters.put("sourceUuid", po.getSourceUuid());
	    parameters.put("associationType", po.getAssociationType());
	    parameters.put("associationLabel", po.getAssociationLabel());
	    parameters.put("entryUuid", po.getEntryUuid());
	    parameters.put("previousVersion", po.getPreviousVersion());
	    parameters.put("originalStatus",po.getOriginalStatus());
	    parameters.put("newStatus", po.getNewStatus());
	    parameters.put("associationPropagation", po.getAssociationPropagation());
	    parameters.put("availabilityStatus", po.getAvailabilityStatus());

		log.info(sql.toString());
	    namedParameterJdbcTemplate.update(sql.toString(), parameters);
	}
	
	public void saveXdsDocumentEntryPO(XdsDocumentEntryPO po)  {

		StringBuilder sql = new StringBuilder();
		sql.append(" INSERT INTO camel_xdsdocumententry (");
		sql.append("createTime,updateTime,classCode,confidentialityCodes,creationTime,formatCode,hash,entryUuid,patientId,uniqueId,homeCommunityId,logicalUuid,version,"
				+ "healthcareFacilityTypeCode,languageCode,mimeType,practiceSettingCode,serviceStartTime,serviceStopTime,size,sourcePatientId,typeCode,uri,repositoryUniqueId,documentEntryType,documentAvailability,availabilityStatus");
		sql.append(") VALUES (");
		sql.append(":createTime,:updateTime,:classCode,:confidentialityCodes,:creationTime,:formatCode,:hash,:entryUuid,:patientId,:uniqueId,:homeCommunityId,:logicalUuid,:version,"
				+ ":healthcareFacilityTypeCode,:languageCode,:mimeType,:practiceSettingCode,:serviceStartTime,:serviceStopTime,:size,:sourcePatientId,:typeCode,:uri,:repositoryUniqueId,:documentEntryType,:documentAvailability,:availabilityStatus");
		sql.append(")");

	    Map<String, Object> parameters = new HashMap<>();
	    parameters.put("createTime", po.getCreateTime());
	    parameters.put("updateTime", po.getUpdateTime());
	    parameters.put("classCode",po.getClassCode());
	    parameters.put("confidentialityCodes", po.getConfidentialityCodes());
	    parameters.put("creationTime", po.getCreationTime());
	    parameters.put("formatCode", po.getFormatCode());
	    parameters.put("hash", po.getHash());	   
	    parameters.put("entryUuid", po.getEntryUuid());
	    parameters.put("patientId", po.getPatientId());
	    parameters.put("uniqueId",po.getUniqueId());
	    parameters.put("homeCommunityId", po.getHomeCommunityId());
	    parameters.put("logicalUuid", po.getLogicalUuid());
	    parameters.put("version", po.getVersion());
	    
	    parameters.put("healthcareFacilityTypeCode", po.getHealthcareFacilityTypeCode());
	    parameters.put("languageCode", po.getLanguageCode());
	    parameters.put("mimeType",po.getMimeType());
	    parameters.put("practiceSettingCode", po.getPracticeSettingCode());
	    parameters.put("serviceStartTime", po.getServiceStartTime());
	    parameters.put("serviceStopTime", po.getServiceStopTime());
	    parameters.put("size", po.getSize());	   
	    parameters.put("sourcePatientId", po.getSourcePatientId());
	    parameters.put("typeCode", po.getTypeCode());
	    parameters.put("uri",po.getUri());
	    parameters.put("repositoryUniqueId", po.getRepositoryUniqueId());
	    parameters.put("documentEntryType", po.getDocumentEntryType());
	    parameters.put("documentAvailability", po.getDocumentAvailability());
	    parameters.put("availabilityStatus", po.getAvailabilityStatus());

		log.info(sql.toString());
	    namedParameterJdbcTemplate.update(sql.toString(), parameters);
	}

	public void saveXdsRepositoryPO(XdsRepositoryPO xdsObject)  {

		StringBuilder sql = new StringBuilder();
		sql.append(" INSERT INTO camel_repository (");
		sql.append("dataId,createTime,updateTime,documentEntryUuid,documentHomeCommunityId,documentUniqueId,fileName,filePath");
		sql.append(") VALUES (");
		sql.append(":dataId,:createTime,:updateTime,:documentEntryUuid,:documentHomeCommunityId,:documentUniqueId,:fileName,:filePath");
		sql.append(")");
		
	    Map<String, Object> parameters = new HashMap<>();
	    parameters.put("dataId", xdsObject.getDataId());
	    parameters.put("createTime", xdsObject.getCreateTime());
	    parameters.put("updateTime", xdsObject.getUpdateTime());
	    parameters.put("documentEntryUuid", xdsObject.getDocumentUniqueId());
	    parameters.put("documentHomeCommunityId", xdsObject.getDocumentHomeCommunityId());
	    parameters.put("documentUniqueId", xdsObject.getDocumentUniqueId());
	    parameters.put("fileName", xdsObject.getFileName());
	    parameters.put("filePath", xdsObject.getFilePath());
		log.info(sql.toString());
	    namedParameterJdbcTemplate.update(sql.toString(), parameters);
	}
	
	public void saveXdsCache(XdsCache xdsObject)  {

		StringBuilder sql = new StringBuilder();
		sql.append(" INSERT INTO camel_xdscache (");
		sql.append("dataId,createTime,updateTime,dataClass,dataType,dataContent,dataEntryUuid");
		sql.append(") VALUES (");
		sql.append(":dataId,:createTime,:updateTime,:dataClass,:dataType,:dataContent,:dataEntryUuid");
		sql.append(")");
		
	    Map<String, Object> parameters = new HashMap<>();
	    parameters.put("dataId", xdsObject.getDataId());
	    parameters.put("createTime", xdsObject.getCreateTime());
	    parameters.put("updateTime", xdsObject.getUpdateTime());
	    parameters.put("dataClass", xdsObject.getDataClass());
	    parameters.put("dataType", xdsObject.getDataType());
	    parameters.put("dataContent", xdsObject.getDataContent());
	    parameters.put("dataEntryUuid", xdsObject.getDataEntryUuid());
		log.info(sql.toString());
	    namedParameterJdbcTemplate.update(sql.toString(), parameters);
	}
	
}
